/*
 * Created on 2006-2-25
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.toyi.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.toyi.util.StringUtil;
/**
 * @author yangbingxp
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class SimpleLogger {
//  ---------------------------------------------------------------------------------------------
	// Class Variables
	//---------------------------------------------------------------------------------------------
	private static final String PROPERTIES_FILENAME = "config/toyi.log.properties";
	private static Logger _auditLog;
	private static Logger _debugLog;

	//---------------------------------------------------------------------------------------------
	// Initializer
	//---------------------------------------------------------------------------------------------
	static
	{
		try
		{
			Properties p = new Properties();			
			InputStream in = null;
			try {
				in = new FileInputStream(PROPERTIES_FILENAME);
				System.out.println("Configuring Log4J using properties file:[" + PROPERTIES_FILENAME + "]");
			} catch (FileNotFoundException e) {
				in = SimpleLogger.class.getResourceAsStream(StringUtil.convertNameForStreamSearch(PROPERTIES_FILENAME));
				System.out.println("Configuring Log4J using properties file:[" + PROPERTIES_FILENAME + "] in classpath");
			}
			finally{
				if(in != null){
					p.load(in);
					in.close();
				}
			}
			PropertyConfigurator.configure(p);
		
			_auditLog = Logger.getLogger("com.toyi.audit");
			_debugLog = Logger.getLogger("com.toyi.debug");
		}
		catch (Exception exc)
		{
			exc.printStackTrace();
		}
	}

	//---------------------------------------------------------------------------------------------
	// Constructors
	//---------------------------------------------------------------------------------------------
	/**
	 * Empty constructor.
	 */
	private SimpleLogger()
	{
	}

	//---------------------------------------------------------------------------------------------
	// Class Methods
	//---------------------------------------------------------------------------------------------
	/**
	 * Prints audit information.
	 * @param statement the statement to print.
	 */
	public static void audit(String statement)
	{
		_auditLog.info(statement);
	}

	/**
	 * Prints debug messages.
	 * @param statement the statement to print.
	 */
	public static void debug(String statement)
	{
		_debugLog.debug(statement);
	}

	/**
	 * Prints error messages.
	 * @param statement the statement to print.
	 */
	public static void error(String statement)
	{
		_debugLog.error(statement);
	}

	/**
	 * Prints information messages.
	 * @param statement the statement to print.
	 */
	public static void info(String statement)
	{
		_debugLog.info(statement);
	}

	/**
	 * Checks whether debug mode is enabled.
	 * @return True is debug mode is on, false otherwise.
	 */
	public static boolean isDebugEnabled()
	{
		return _debugLog.isDebugEnabled();
	}

	/**
	 * Prints the stack trace.
	 * @param exc the stack trace to print.
	 */
	public static void printStackTrace(Exception exc)
	{
		_debugLog.error("----------Exception start---------------");
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		exc.printStackTrace(pw);
		_debugLog.error(sw.getBuffer().toString());
		_debugLog.error("-----------Exception end----------------");
	}

	/**
	 * Prints out the value of a parameter.
	 */
	public static void printVar(String var, Object varObj)
	{
		_debugLog.debug("--Value of " + var + " is [" + (String) varObj + "]");
	}
}
